import request from '@/utils/request'

const URL_PREFIX = '/iam/role'

const fetch = function(id) {
  return request({
    url: `${URL_PREFIX}/${id}`,
    method: 'post'
  })
}

const fetchList = function(queryParams) {
  return request({
    url: `${URL_PREFIX}/list?page=${queryParams.pageNum}&pageSize=${queryParams.pageSize}`,
    method: 'post',
    data: queryParams
  })
}

const save = function(data) {
  if (data.id === undefined) {
    return request({
      url: `${URL_PREFIX}/create`,
      method: 'post',
      data: data
    })
  } else {
    return request({
      url: `${URL_PREFIX}/edit`,
      method: 'post',
      data: data
    })
  }
}

const remove = function(ids) {
  return request({
    url: `${URL_PREFIX}/remove`,
    method: 'post',
    data: { ids }
  })
}

const fetchRoleMenu = function(id) {
  return request({
    url: `${URL_PREFIX}/role-menu/${id}`,
    method: 'post'
  })
}

const grantRoleMenu = function(rid, appMenuIds, webMenuIds) {
  return request({
    url: `${URL_PREFIX}/menu-grant/${rid}`,
    method: 'post',
    data: {
      appMenus: appMenuIds,
      webMenus: webMenuIds
    }
  })
}

export default {
  fetch,
  save,
  remove,
  fetchList,
  fetchRoleMenu,
  grantRoleMenu
}
